package fhv.itb.ec.auction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;

public class AuctionImpl1 extends UnicastRemoteObject {
	
	private static final long serialVersionUID = 1L;
	//Vector for holding Items
	private Vector<Item> m_items = null;
	//Vector for holding Bidders
	private Vector<Bidder> m_bidders = null;
	
	private String m_lastBidItem = "";
	private int m_lastBid = 0;
	
	protected AuctionImpl1() throws IOException {
		super();
		
		m_items = new Vector<Item>();
		m_bidders = new Vector<Bidder>();
		
		//Reading the items from file and storing them in vector
		File items = new File("Items.txt");
		BufferedReader in = null;
		
		try {
			in = new BufferedReader(new FileReader(items));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		if (in != null) {

			String line = in.readLine();
			int count = 1;
			
			while (line != null) {
				Item item = new Item();
				
				while (count <= 3) {
					
					if (count == 1) {
						item.setTitle(line);
					} else if (count == 2) {
						item.setDescription(line);
					} else if (count == 3) {
						item.setCurrentPrice(Integer.parseInt(line));
					}
					count++;
					line = in.readLine();
				}
				count = 1;
				m_items.add(item);
			}
			in.close();
		}
	}

	
	public String bid(String bidderName, String item, int price) throws RemoteException {
		//Controlling if bidder has joined the auction
		if (m_bidders != null) {
			boolean found = false;
			for (int i = 0; (found == false) && (i < m_bidders.size()); i++) {
				if (m_bidders.elementAt(i).getName().equals(bidderName) &&
						m_bidders.elementAt(i).getAuction().equals(item)) {
					
					if (m_bidders.elementAt(i).isJoined() == false) {
						return "bidding failed " + bidderName + " has not joined auction for " + item;
					} else if (m_bidders.elementAt(i).isJoined() == true){
						found = true;
					}
				}
			}
			if (found == false) {
				return "Sorry " + bidderName + " is not registered, please join auction for " + item;
			}
			//If bidder has joined the auction
			if (m_items != null) {
				boolean exist = false;
				for (int i = 0; i < m_items.size(); i++) {
					//controlling if item which client wants to bid exists
					if (m_items.elementAt(i).getTitle().equals(item)) {
						exist = true;
						//controlling if the bidding is not lower than actual price
						if (price > m_items.elementAt(i).getCurrentPrice()) {
							m_items.elementAt(i).setCurrentPrice(price);
							m_lastBidItem = item;
							m_lastBid = price;
							return "Success, Bidder " + bidderName + " bidden " + price + " for Item " + item;
						} else {
							return "Bid for Item " + item + " must be bigger as the current bid";
						}
					}
				}
				if (exist == false) {
					return "Sorry " + bidderName + ", Auction " + item + " does not exist";
				}
				
			}
			
		}
		return "bidding failed";
	}

	
	public String joinAuction(String bidderName, String auctionName) throws RemoteException {
		//Controlling if auction exists
		if (m_items != null) {
			boolean exist = false;
			for (int i = 0; i < m_items.size(); i++) {
				if (m_items.elementAt(i).getTitle().equals(auctionName)) {
					exist = true;
				}
			}
			if (exist == false) {
				return "Sorry " + bidderName + ", Auction " + auctionName + " does not exist";
			}
		}
		
		//Controlling if bidder is in the vector, when found set him as joined
		if (m_bidders != null) {
			boolean found = false;
			for (int i = 0; (found == false) && (i < m_bidders.size()); i++) {
				if (m_bidders.elementAt(i).getName().equals(bidderName) &&
						m_bidders.elementAt(i).getAuction().equals(auctionName)) {
					if (m_bidders.elementAt(i).isJoined() == false) {
						m_bidders.elementAt(i).setJoined(true);
						found = true;
						return bidderName + " has joined auction " + auctionName;
					} else {
						found = true;
						return bidderName + " has already joined auction " + auctionName;
					}
				}
			}
			//if bidder is not found he is added
			if (found == false) {
				Bidder bidder = new Bidder(bidderName, auctionName, true);
				m_bidders.add(bidder);
				return bidderName + " is new and has joined auction " + auctionName;
			}
		}
		return "joining auction failed";
	}

	
	public String leaveAuction(String bidderName, String auctionName) throws RemoteException {
		//Control if bidder is in vector, if he is then set joined variable false
		if (m_bidders != null) {
			boolean found = false;
			for (int i = 0; (found == false) && (i < m_bidders.size()); i++) {
				if (m_bidders.elementAt(i).getName().equals(bidderName) &&
						m_bidders.elementAt(i).getAuction().equals(auctionName)) {
					if (m_bidders.elementAt(i).isJoined() == true) {
						m_bidders.elementAt(i).setJoined(false);
						found = true;
						return bidderName + " has left auction " + auctionName;
					} else {
						found = true;
						return bidderName + " has already left auction " + auctionName;
					}
				}
			}
			if (found == false) {
				return bidderName + " does not exist";
			}
		}
		return "leaving auction failed";
	}


	public Vector<Item> getItems() {
		return m_items;
	}

	public void setItems(Vector<Item> items) {
		m_items = items;
	}


	public String getLastBidItem() {
		return m_lastBidItem;
	}


	public int getLastBid() {
		return m_lastBid;
	}

}
